#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "dialog.h"
#include "utils.h"

#include <QDebug>
#include <QFile>
#include <QCoreApplication>
#include <QTextStream>
#include <QProcess>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->spinBox->setRange(5,20);
    ui->spinBox_2->setRange(2,8);
    ui->spinBox_2->setSingleStep(0.5);
    ui->spinBox_2->setSuffix(" cm");
    ui->spinBox->setValue(10);
    ui->spinBox_2->setValue(4);
    ui->lineEdit->setEnabled(false);
    ui->toolButton->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionQuit_triggered()
{
    QApplication::quit();
}

void MainWindow::on_actionAbout_triggered()
{
    Dialog * about = new Dialog(this);
    about->show();
}

void MainWindow::on_actionNew_2_triggered()
{

}

void MainWindow::on_checkBox_toggled(bool checked)
{
    ui->plainTextEdit_4->setEnabled(checked);
}

void MainWindow::on_pushButton_clicked()
{
    QString type = ui->comboBox->currentText();
    QString paper = ui->comboBox_2->currentText();
    QString orientation = ui->comboBox_3->currentText();
    QString title = ui->plainTextEdit->toPlainText();
    QString description = ui->plainTextEdit_2->toPlainText();
    QString phone_number = ui->plainTextEdit_3->toPlainText();
    QString reverse = ui->plainTextEdit_4->toPlainText();
    QString stud_number = QString::number(ui->spinBox->value());
    QString stud_length = QString::number(ui->spinBox_2->value());
    QString enable_reverse = "";
    QString landscape = "";
    QString pic_path = ui->lineEdit->text();
    QString picture = "";

    if (!ui->checkBox->isChecked()) {
         enable_reverse = "%";
    }

    if(ui->comboBox_3->currentText() == "Landscape") {
        landscape = "\\usepackage[landscape]{geometry}\n";
    }

    if(ui->checkBox_2->isChecked()) {
        picture = "\\begin{figure}[h]\n"
                    "\\centering\n"
                    "\\includegraphics[width=0.2\\textwidth]{"+pic_path+"}\n"
                    "\\end{figure}\n";
    }

    QString out = "\\documentclass["+paper+"paper]{article}\n" \

            "\\usepackage[addmargin]{stubs}\n" \
            "\\usepackage[utf8x]{inputenc}\n" \
            ""+landscape+"\n"

            "\\begin{document}\n" \
            "\\thispagestyle{empty} \\pagestyle{empty}\n" \
            "\\begin{center}\n" \
            "\\Huge \\textbf{"+title+"}\\\\\n" \
            "~\\\\\n"
            "\\Large "+description+"\n" \
            "\\end{center}\n" \
            ""+picture+"\n" \
            "\\stubs["+stud_number+"]{"+stud_length+"cm}"+enable_reverse+"[{\\raggedright "+reverse+"}]\n" \
              "{\\raggedright "+phone_number+"}\n" \
            "\\end{document}\n";

    qDebug() << out;

    QFile file("out.tex");
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out_tex(&file);
    out_tex << out;

    // optional, as QFile destructor will already do it:
    file.close();

    QProcess *process = new QProcess(this);
    QString compile = "pdflatex out.tex";
    process->start(compile);



}

void MainWindow::on_checkBox_2_toggled(bool checked)
{
    ui->lineEdit->setEnabled(checked);
    ui->toolButton->setEnabled(checked);
}

void MainWindow::on_toolButton_clicked()
{
    ui->lineEdit->setText("");
    QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open File"),"/path/to/file/",tr("PNG Files (*.png)"));

    if( fileNames.count() > 0){
        ui->lineEdit->setText(fileNames.at(0));
    }
}



void MainWindow::on_plainTextEdit_textChanged()
{
    Utils::limitTextEdit(ui->plainTextEdit, 40);
}

void MainWindow::on_plainTextEdit_2_textChanged()
{
    Utils::limitTextEdit(ui->plainTextEdit_2, 40);
}


